public class Solution833 {
    public String findReplaceString(String s, int[] indices, String[] sources, String[] targets) {
        int n=indices.length;
        for (int i=0;i<n;i++){
            for (int j=i+1;j<n;j++){
                if (indices[i]>indices[j]){
                    int t=indices[i];
                    indices[i]=indices[j];
                    indices[j]=t;
                    String a=sources[i];
                    sources[i]=sources[j];
                    sources[j]=a;
                    a=targets[i];
                    targets[i]=targets[j];
                    targets[j]=a;
                }
            }
        }
        StringBuilder stringBuilder=new StringBuilder();
        int cur=0;
        for (int i=0;i<indices.length;i++){
            while (cur<indices[i]){
                stringBuilder.append(s.charAt(cur));
                cur++;
            }
            boolean flag=true;
            for (int j=indices[i];j<indices[i]+sources[i].length();j++){
                if (j>=s.length() || s.charAt(j)!=sources[i].charAt(j-indices[i])){
                    flag=false;
                    break;
                }
            }
            if (flag){
                stringBuilder.append(targets[i]);
                cur=indices[i]+sources[i].length();
            }
        }
        while (cur<s.length()){
            stringBuilder.append(s.charAt(cur));
            cur++;
        }
        return stringBuilder.toString();
    }

    public static void main(String[] args) {
        System.out.println(new Solution833().findReplaceString("abcd",new int[]{0,2},new String[]{"a","cd"},new String[]{"eee","ffff"}));
    }
}
